package org.csu.bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.bbs.entity.Collection;
import org.csu.bbs.entity.Posts;
import org.csu.bbs.entity.Signon;
import org.csu.bbs.persistence.CollectionMapper;
import org.csu.bbs.persistence.PostsMapper;
import org.csu.bbs.result.BetterResult;
import org.csu.bbs.result.Result;
import org.csu.bbs.result.User;
import org.csu.bbs.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class CollectionServiceImpl  implements CollectionService {
    @Autowired
    CollectionMapper collectionMapper;

    @Override
    public Result deleteCollection(String userid, String postid) {
        UpdateWrapper<Collection> updateWrapper = new UpdateWrapper<>();
        Map<String,Object> map=new HashMap<>();
        map.put("userid",userid);
        map.put("postid",postid);
        updateWrapper.allEq(map);
        try {
            collectionMapper.delete(updateWrapper);
            return new Result(200);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(400);
        }
    }

    @Override
    public Result addCollection(Collection collection) {
        try{
            collectionMapper.insert(collection);
            return new Result(200);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(400);
        }
    }

    @Override
    public Collection iscollectioned(String userid, String postid) {
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map=new HashMap<>();
        map.put("userid",userid);
        map.put("postid",postid);
        queryWrapper.allEq(map);
        return collectionMapper.selectOne(queryWrapper);
    }

    @Override
    public BetterResult<Integer> GetPostStars(Integer pid) {
        int stars = 0;
        try {
            QueryWrapper<Collection> collectionQueryWrapper = new QueryWrapper<>();
            collectionQueryWrapper.eq("postid", pid);
            stars = collectionMapper.selectList(collectionQueryWrapper).size();
            return new BetterResult<>(stars, 200);
        } catch (Exception e) {
            e.printStackTrace();
            return new BetterResult<>(stars, 400);
        }
    }
}
